package edu.mtu.citizenscience.ethnography;

import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import edu.mtu.citizenscience.ethnography.database.Interview;
import edu.mtu.citizenscience.ethnography.database.InterviewAudio;
import edu.mtu.citizenscience.ethnography.database.InterviewDataService;
import edu.mtu.citizenscience.ethnography.database.InterviewFileStatus;
import edu.mtu.citizenscience.ethnography.util.DataServiceActivity;
import edu.mtu.citizenscience.ethnography.util.Util;
import edu.mtu.citizenscience.ethnography.util.Util.LocationTriple;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.graphics.Typeface;
import android.location.Location;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.DialogFragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

public class AudioListActivity extends DataServiceActivity implements MediaPlayer.OnCompletionListener, MediaPlayer.OnErrorListener{

	/*
	 * FUNCTIONAL NOTES: These should be transferred to external documentation
	 * as soon as possible
	 * 
	 * If an error occurs:
	 * 	A dialog should pop up and all play operations should be blocked
	 * 
	 * If the user presses play/pause while nothing is playing:
	 * 	Button should say "play"
	 * 	Create media listener from file.
	 * 	Set next media listener from next valid file.
	 *  Set current index, current media listener and next media listener fields
	 *  
	 * 
	 */

	private static final String LOG_TAG = "AudioListActivity";

	// ID of the interview
	private int interviewID;

	// ID/view for the user's selection
	private View selectedItemView;
	private int selectedItemPos;

	// Views
	private ListView listView;
	private Button playPauseButton;
	private Button deleteButton;

	// Color vars
	private int unselectedItemColor;
	private int selectedItemColor;
	private int unselectedTextColor;
	private int selectedTextColor;

	// List adapter
	private AudioListAdapter adapter;

	// Media player-related
	private int currentIndex;
	private MediaPlayer currentMediaPlayer = null;

	// For automatically updating the time
	private Handler handler;
	private Runnable progressUpdater;
	private static final int HANDLER_DELAY = 1000;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_audio_list);

		unselectedItemColor = getResources().getColor(R.color.default_background_color);
		selectedItemColor = getResources().getColor(R.color.item_selected_color);
		unselectedTextColor = getResources().getColor(R.color.default_text_color);
		selectedTextColor = getResources().getColor(R.color.selection_text_color);

		playPauseButton = (Button) findViewById(R.id.playButton);
		deleteButton = (Button) findViewById(R.id.audioDeleteButton);

		listView = (ListView) findViewById(R.id.audioList);
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position,
					long id) {
				if (position == selectedItemPos && adapter.getItem(selectedItemPos).getFileStatus() != InterviewFileStatus.NOT_FOUND) {
					performPlay(playPauseButton);
				} else {
					if (selectedItemView != null) {
						selectedItemView.setBackgroundColor(unselectedItemColor);
						TextView datestampLabel = (TextView) selectedItemView.findViewById(R.id.datestamp_label);
						TextView datestampField = (TextView) selectedItemView.findViewById(R.id.datestamp_field);
						TextView startLabel = (TextView) selectedItemView.findViewById(R.id.start_label);
						TextView startField = (TextView) selectedItemView.findViewById(R.id.start_location);
						TextView endLabel = (TextView) selectedItemView.findViewById(R.id.end_label);
						TextView endField = (TextView) selectedItemView.findViewById(R.id.end_location);

						datestampLabel.setTextColor(unselectedTextColor);
						datestampField.setTextColor(unselectedTextColor);
						startLabel.setTextColor(unselectedTextColor);
						startField.setTextColor(unselectedTextColor);
						endLabel.setTextColor(unselectedTextColor);
						endField.setTextColor(unselectedTextColor);
					}

					selectedItemView = view;
					selectedItemPos = position;

					if (currentMediaPlayer != null) {
						// Have an active media player
						if (currentMediaPlayer.isPlaying()) {
							// Media player is playing
							if (selectedItemPos != currentIndex) {
								// Selecting a different item, so indicate that
								// pressing button will play this instead
								playPauseButton.setText(R.string.play_button_label);
							} else {
								// Selecting currently playing item, so indicate that
								// pressing button will pause
								playPauseButton.setText(R.string.pause_button_label);
							}
						} else {
							playPauseButton.setText(R.string.play_button_label);
						}
					}

					InterviewAudio audio = adapter.getItem(selectedItemPos);
					if (audio.getFileStatus() == InterviewFileStatus.NOT_FOUND) {
						playPauseButton.setEnabled(false);
					} else {
						playPauseButton.setEnabled(true);
					}

					selectedItemView.setBackgroundColor(selectedItemColor);
					TextView datestampLabel = (TextView) selectedItemView.findViewById(R.id.datestamp_label);
					TextView datestampField = (TextView) selectedItemView.findViewById(R.id.datestamp_field);
					TextView startLabel = (TextView) selectedItemView.findViewById(R.id.start_label);
					TextView startField = (TextView) selectedItemView.findViewById(R.id.start_location);
					TextView endLabel = (TextView) selectedItemView.findViewById(R.id.end_label);
					TextView endField = (TextView) selectedItemView.findViewById(R.id.end_location);

					datestampLabel.setTextColor(selectedTextColor);
					datestampField.setTextColor(selectedTextColor);
					startLabel.setTextColor(selectedTextColor);
					startField.setTextColor(selectedTextColor);
					endLabel.setTextColor(selectedTextColor);
					endField.setTextColor(selectedTextColor);

					deleteButton.setEnabled(true);
				}
			}
		}
				);

		progressUpdater = new Runnable() {

			@Override
			public void run() {
				if (currentMediaPlayer != null) {
					String outString = formatRunningTime(currentMediaPlayer, true);

					View itemView = listView.getChildAt(currentIndex);
					TextView textView = (TextView) itemView.findViewById(R.id.time_remaining);
					textView.setText(outString);

					handler.postDelayed(progressUpdater, HANDLER_DELAY);
				}
			}
		};
		handler = new Handler();

		if (savedInstanceState == null) {
			Bundle extras = getIntent().getExtras();
			if (extras != null) {
				interviewID = extras.getInt(Util.ID_KEY, -1);
			} else {
				Log.e(LOG_TAG, "Missing interviewID in extras");
				AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext());
				builder.setMessage(R.string.activity_start_error);
				builder.create().show();
				finish();
			}
		} else {
			interviewID = savedInstanceState.getInt(Util.ID_KEY, -1);
		}
	}

	private String formatRunningTime(MediaPlayer m, boolean playing) {
		String outString;

		int playerDuration = currentMediaPlayer.getDuration();

		if (playing){
			int currentPlayerTime = currentMediaPlayer.getCurrentPosition();
			String playerDurationStr;
			String currentTimeStr;
			
			if (Util.isOverOneHour(playerDuration)) {
				playerDurationStr = Util.formatTimeWithHours(playerDuration);
				currentTimeStr = Util.formatTimeWithHours(currentPlayerTime); 
			} else {
				playerDurationStr = Util.formatTimeNoHours(playerDuration);
				currentTimeStr = Util.formatTimeNoHours(currentPlayerTime);
			}
			outString = String.format(Locale.US, "%s / %s", 
					currentTimeStr,
					playerDurationStr);
		} else {
			String playerDurationStr;
			if (Util.isOverOneHour(playerDuration)) {
				playerDurationStr = Util.formatTimeWithHours(playerDuration);
			} else {
				playerDurationStr = Util.formatTimeNoHours(playerDuration);
			}
			
			outString = String.format(Locale.US, " / %s", playerDurationStr);
		}

		return outString;
	}

	@Override
	protected void onStart() {
		selectedItemView = null;
		selectedItemPos = -1;

		playPauseButton.setEnabled(false);
		playPauseButton.setText(R.string.play_button_label);
		deleteButton.setEnabled(false);

		super.onStart();
	}

	@Override
	protected void onSaveInstanceState(Bundle outstate) {
		outstate.putInt(Util.ID_KEY, interviewID);
		super.onSaveInstanceState(outstate);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_audio_list, menu);
		return true;
	}

	@Override
	protected void onStop() {
		handler.removeCallbacks(progressUpdater);
		if (currentMediaPlayer != null) {
			currentMediaPlayer.release();
		}
		super.onStop();
	}

	@Override
	protected void onPause() {
		handler.removeCallbacks(progressUpdater);
		super.onPause();
	}

	@Override
	protected void onResume() {
		super.onResume();
		if (adapter != null) {
			adapter.notifyDataSetChanged();
		}
		if (currentMediaPlayer != null && currentMediaPlayer.isPlaying()) {
			handler.postDelayed(progressUpdater, HANDLER_DELAY);
		}
	}

	@Override
	protected void performDatabaseOperation() {
		Interview i = getService().getInterview(interviewID);
		List<InterviewAudio> audio = getService().audioFilesForInterview(i);

		AudioListAdapter adapter = new AudioListAdapter(getApplicationContext(), audio);
		adapter.sort(new Comparator<InterviewAudio>() {

			@Override
			public int compare(InterviewAudio lhs, InterviewAudio rhs) {
				return lhs.getDate().compareTo(rhs.getDate());
			}

		});

		listView.setAdapter(adapter);
		if (adapter.getCount() > 0) {
			playPauseButton.setEnabled(true);
		}

		this.adapter = adapter;
	}

	public void performPlay(View view) {
		if (currentMediaPlayer == null) {
			// No active player, nothing playing
			if (selectedItemView != null) {
				// If something selected, start from that
				currentIndex = selectedItemPos;
				currentMediaPlayer = prepareFromAdapter(selectedItemPos);
				if (currentMediaPlayer != null) {
					currentMediaPlayer.start();
					handler.post(progressUpdater);
				}
			} else {
				if (adapter.getCount() > 0) {
					// If nothing selected, start from first (do nothing if nothing to play)
					currentIndex = 0;
					currentMediaPlayer = prepareFromAdapter(getNextPosition(-1));
					if (currentMediaPlayer != null) {
						currentMediaPlayer.start();
						handler.post(progressUpdater);
					}
				}
			}
			playPauseButton.setText(R.string.pause_button_label);
		} else {
			// Currently active media player; pause or resume
			if (currentMediaPlayer.isPlaying()) {
				if (currentIndex == selectedItemPos) {
					// If the current one is selected, pause it
					handler.removeCallbacks(progressUpdater);
					currentMediaPlayer.pause();
					playPauseButton.setText(R.string.play_button_label);
				} else {
					// If another one is selected, go to that one
					handler.removeCallbacks(progressUpdater);
					currentMediaPlayer.stop();
					String outString = formatRunningTime(currentMediaPlayer, false);
					View itemView = listView.getChildAt(currentIndex);
					TextView textView = (TextView) itemView.findViewById(R.id.time_remaining);
					textView.setText(outString);
					currentMediaPlayer.release();
					currentIndex = selectedItemPos;
					currentMediaPlayer = prepareFromAdapter(currentIndex);
					currentMediaPlayer.start();
					handler.post(progressUpdater);
				}
			} else {
				if (currentIndex != selectedItemPos) {
					currentMediaPlayer.stop();
					String outString = formatRunningTime(currentMediaPlayer, false);
					View itemView = listView.getChildAt(currentIndex);
					TextView textView = (TextView) itemView.findViewById(R.id.time_remaining);
					textView.setText(outString);
					currentMediaPlayer.release();
					currentIndex = selectedItemPos;
					currentMediaPlayer = prepareFromAdapter(currentIndex);
					currentMediaPlayer.start();
					handler.post(progressUpdater);
				}
				currentMediaPlayer.start();
				handler.post(progressUpdater);
				playPauseButton.setText(R.string.pause_button_label);
			}
		}
	}

	private int getNextPosition(int currentPosition) {
		int index = currentPosition + 1;

		for (index = currentPosition + 1; 
				index < adapter.getCount() && adapter.getItem(index).getFileStatus() != InterviewFileStatus.FOUND; 
				index++)
			; // null statement

		return index;
	}

	private MediaPlayer prepareFromAdapter(int position) {
		MediaPlayer player;
		if (position < adapter.getCount()) {
			player = new MediaPlayer();
			player.setAudioStreamType(AudioManager.STREAM_MUSIC);
			player.setOnCompletionListener(this);
			player.setOnErrorListener(this);
			try {
				Uri fileUri = Uri.parse(adapter.getItem(position).getAudioFile().getAbsolutePath());
				player.setDataSource(getApplicationContext(), fileUri);
				player.prepare();
			} catch (IOException e) {
				Log.e(LOG_TAG, "Preparing the next player failed", e);
				setErrorState();
				player = null;
			}
		} else {
			player = null;
		}
		return player;
	}

	private void setErrorState() {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setMessage(getString(R.string.media_player_error));
		builder.create().show();
		playPauseButton.setEnabled(false);
	}

	public void deleteAudio(View view) {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setMessage(getString(R.string.delete_audio_message));
		builder.setPositiveButton(R.string.delete_ok, new OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				try {
					if (currentMediaPlayer != null) {
						if (selectedItemPos == currentIndex) {
							handler.removeCallbacks(progressUpdater);
							currentMediaPlayer.stop();
							currentMediaPlayer.release();
							currentMediaPlayer = null;
							playPauseButton.setText(R.string.play_button_label);
						} else if (selectedItemPos < currentIndex) {
							currentIndex--;
						}
					}
					getService().removeAudioFile(adapter.getItem(selectedItemPos));
					adapter.remove(adapter.getItem(selectedItemPos));
					adapter.notifyDataSetChanged();

					selectedItemPos = -1;
					selectedItemView = null;
					deleteButton.setEnabled(false);
					if (adapter.getCount() == 0) {
						playPauseButton.setEnabled(false);
					}
				} catch (IOException e) {
					Log.e(LOG_TAG, 
							String.format("Deleting audio %d threw an exception", 
									selectedItemPos), 
									e);
					fail(R.string.io_error);
				}
			}
		});
		builder.setNegativeButton(R.string.cancel, new OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				// Do nothing

			}
		});
		builder.create().show();
	}

	private static class AudioListAdapter extends ArrayAdapter<InterviewAudio> {
		private final Context context;

		public AudioListAdapter(Context context, List<InterviewAudio> values) {
			super(context, R.layout.audio_item_view, values);
			this.context = context;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			View itemView = inflater.inflate(R.layout.audio_item_view, parent, false);

			TextView datestampField = (TextView) itemView.findViewById(R.id.datestamp_field);
			TextView startField = (TextView) itemView.findViewById(R.id.start_location);
			TextView endField = (TextView) itemView.findViewById(R.id.end_location);

			InterviewAudio audio = getItem(position);
			Date date = audio.getDate();
			if (date != null) {
				DateFormat dateFormat = new SimpleDateFormat("yyyy.MM.dd 'at' HH:mm:ss z", Locale.US);
				datestampField.setText(dateFormat.format(date));
			} else {
				datestampField.setTypeface(null, Typeface.ITALIC);
				datestampField.setText(R.string.datestamp_default);
			}

			Location startLocation = audio.getStartLocation();
			if (startLocation != null) {
				String text = Location.convert(startLocation.getLatitude(), Location.FORMAT_SECONDS) +
						", " + Location.convert(startLocation.getLongitude(), Location.FORMAT_SECONDS);
				startField.setText(text);
			} else {
				startField.setTypeface(null, Typeface.ITALIC);
				startField.setText(R.string.location_default);
			}

			Location endLocation = audio.getEndLocation();
			if (startLocation != null) {
				String text = Location.convert(endLocation.getLatitude(), Location.FORMAT_SECONDS) +
						", " + Location.convert(endLocation.getLongitude(), Location.FORMAT_SECONDS);
				endField.setText(text);
			} else {
				endField.setTypeface(null, Typeface.ITALIC);
				endField.setText(R.string.location_default);
			}

			return itemView;
		}

	}

	@Override
	public boolean onError(MediaPlayer mp, int what, int extra) {
		setErrorState();
		return false;
	}

	@Override
	public void onCompletion(MediaPlayer mp) {
		if (mp == currentMediaPlayer) {
			// Have this block so we don't advance if an error was thrown in preparation
			handler.removeCallbacks(progressUpdater);
			currentMediaPlayer.stop();
			String outString = formatRunningTime(currentMediaPlayer, false);
			View itemView = listView.getChildAt(currentIndex);
			TextView textView = (TextView) itemView.findViewById(R.id.time_remaining);
			textView.setText(outString);
			currentMediaPlayer.release();
			int nextIndex = getNextPosition(currentIndex);
			MediaPlayer nextMediaPlayer = prepareFromAdapter(nextIndex);
			currentMediaPlayer = nextMediaPlayer;
			currentIndex = nextIndex;
			if (nextMediaPlayer != null) {
				nextMediaPlayer.start();
				handler.post(progressUpdater);
			}
		}
		mp.release();
	}

	public void editAudio(View view) {
		EditAudioFragment fragment = new EditAudioFragment();
		Bundle args = new Bundle();
		args.putInt(EditAudioFragment.ID, interviewID);
		args.putString(EditAudioFragment.NAME, adapter.getItem(selectedItemPos).getAudioFile().getName());
		fragment.setArguments(args);
		fragment.show(getSupportFragmentManager(), EditAudioFragment.FRAG_TAG);
	}
	
	public static class EditAudioFragment extends DialogFragment {
		@SuppressWarnings("hiding")
		private static final String LOG_TAG = "AudioDialogFragment";
		public static final String FRAG_TAG = LOG_TAG;
		
		private static final String ID = "id";
		private static final String NAME = "filename";
		
		private InterviewAudio audio;
		
		private EditText sLatDeg;
		private EditText sLatMin;
		private EditText sLatSec;
		
		private EditText sLonDeg;
		private EditText sLonMin;
		private EditText sLonSec;
		
		private EditText eLatDeg;
		private EditText eLatMin;
		private EditText eLatSec;
		
		private EditText eLonDeg;
		private EditText eLonMin;
		private EditText eLonSec;
		
		InterviewDataService service;
		
		@Override
		public Dialog onCreateDialog(Bundle savedInstanceState) {
			AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
			LayoutInflater inflater = getActivity().getLayoutInflater();
			int id = getArguments().getInt(ID, -1);
			String name = getArguments().getString(NAME);
			
			builder.setView(inflater.inflate(R.layout.dialog_audio, null));
			
			service = ((DataServiceActivity)getActivity()).getService();
			Interview i = service.getInterview(id);
			audio = service.audioFileForInterview(i, name);
			
			builder.setPositiveButton(R.string.edit_ok, new OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					LocationTriple triple;
					String text;
					
					triple = new LocationTriple();
					if (!sLatDeg.getText().toString().equals("") && !sLonDeg.getText().toString().equals("")) {
						Location location = new Location("Editor");
						
						text = sLatDeg.getText().toString();
						triple.degrees = Integer.valueOf(text);
						text = sLatMin.getText().toString();
						if (!text.equals("")) {
							triple.minutes = Integer.valueOf(text);
						} else {
							triple.minutes = 0;
						}
						text = sLatSec.getText().toString();
						if (!text.equals("")) {
							triple.seconds = Double.valueOf(text);
						} else {
							triple.seconds = 0;
						}
						location.setLatitude(triple.doubleValue());
						
						text = sLonDeg.getText().toString();
						triple.degrees = Integer.valueOf(text);
						text = sLonMin.getText().toString();
						if (!text.equals("")) {
							triple.minutes = Integer.valueOf(text);
						} else {
							triple.minutes = 0;
						}
						text = sLonSec.getText().toString();
						if (!text.equals("")) {
							triple.seconds = Double.valueOf(text);
						} else {
							triple.seconds = 0;
						}
						location.setLongitude(triple.doubleValue());
						
						audio.setStartLocation(location);
						
					} else {
						audio.setStartLocation(null);
					}
					
					if (!eLatDeg.getText().toString().equals("") && !eLonDeg.getText().toString().equals("")) {
						Location location = new Location("Editor");
						
						text = eLatDeg.getText().toString();
						triple.degrees = Integer.valueOf(text);
						text = eLatMin.getText().toString();
						if (!text.equals("")) {
							triple.minutes = Integer.valueOf(text);
						} else {
							triple.minutes = 0;
						}
						text = eLatSec.getText().toString();
						if (!text.equals("")) {
							triple.seconds = Double.valueOf(text);
						} else {
							triple.seconds = 0;
						}
						location.setLatitude(triple.doubleValue());
						
						text = eLonDeg.getText().toString();
						triple.degrees = Integer.valueOf(text);
						text = eLonMin.getText().toString();
						if (!text.equals("")) {
							triple.minutes = Integer.valueOf(text);
						} else {
							triple.minutes = 0;
						}
						text = eLonSec.getText().toString();
						if (!text.equals("")) {
							triple.seconds = Double.valueOf(text);
						} else {
							triple.seconds = 0;
						}
						location.setLongitude(triple.doubleValue());
						
						audio.setEndLocation(location);
						
					} else {
						audio.setEndLocation(null);
					}
					
					service.saveAudioFile(audio);
				}
			});
			
			builder.setNeutralButton(R.string.edit_cancel, new OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					// Do nothing
				}
			});
			
			AlertDialog dialog = builder.show();
			sLatDeg = (EditText) dialog.findViewById(R.id.sLatDegrees);
			sLatMin = (EditText) dialog.findViewById(R.id.sLatMinutes);
			sLatSec = (EditText) dialog.findViewById(R.id.sLatSeconds);
			sLonDeg = (EditText) dialog.findViewById(R.id.sLonDegrees);
			sLonMin = (EditText) dialog.findViewById(R.id.sLonMinutes);
			sLonSec = (EditText) dialog.findViewById(R.id.sLonSeconds);
			if (audio.getStartLocation() != null) {
				LocationTriple latitude = new LocationTriple(audio.getStartLocation().getLatitude());
				sLatDeg.setText(String.valueOf(latitude.degrees));
				sLatMin.setText(String.valueOf(latitude.minutes));
				sLatSec.setText(String.valueOf(latitude.seconds));
				
				LocationTriple longitude = new LocationTriple(audio.getStartLocation().getLongitude());
				sLonDeg.setText(String.valueOf(longitude.degrees));
				sLonMin.setText(String.valueOf(longitude.minutes));
				sLonSec.setText(String.valueOf(longitude.seconds));
			}
			eLatDeg = (EditText) dialog.findViewById(R.id.eLatDegrees);
			eLatMin = (EditText) dialog.findViewById(R.id.eLatMinutes);
			eLatSec = (EditText) dialog.findViewById(R.id.eLatSeconds);
			eLonDeg = (EditText) dialog.findViewById(R.id.eLonDegrees);
			eLonMin = (EditText) dialog.findViewById(R.id.eLonMinutes);
			eLonSec = (EditText) dialog.findViewById(R.id.eLonSeconds);
			if (audio.getEndLocation() != null) {
				LocationTriple latitude = new LocationTriple(audio.getEndLocation().getLatitude());
				eLatDeg.setText(String.valueOf(latitude.degrees));
				eLatMin.setText(String.valueOf(latitude.minutes));
				eLatSec.setText(String.valueOf(latitude.seconds));
				
				LocationTriple longitude = new LocationTriple(audio.getEndLocation().getLongitude());
				eLonDeg.setText(String.valueOf(longitude.degrees));
				eLonMin.setText(String.valueOf(longitude.minutes));
				eLonSec.setText(String.valueOf(longitude.seconds));
			}
			
			return dialog;
		}
	}

}
